$(document).ready(function () {

    $('#pop-wrapper select').select0();

    $('#pop-wrapper .x-4-move').dragging({
        move: 'both',
        randomPosition: false,
        handler: '.panel-heading'
    });

    $('#pop-wrapper span.file-choice-btn, #pop-wrapper .image-container').off('click').on('click', function () {
        $('#pop-wrapper form.fup input[name=file]').trigger('click');
    });

    /*上传图片*/
    $('#pop-wrapper .btn-4-fup').off('change').on('change', function () {
        var form = $('#pop-wrapper form.fup');
        ajaxMultiSubmitResponseJSON({
            url: 'upLoad/fupRespJson.htm',
            form: form,
            success: function (data) {
                if (data.status == 0) {
                    var imgUri = data.data;
                    var imageHtm = '<a class="add-pictures" style="margin-bottom:0;"><img width="48" src="' + imgUri + '" /></a>';
                    $('#pop-wrapper .image-container').html(imageHtm);
                    $('#pop-wrapper form.common-form input[name=avatar]').val(imgUri);
                }
            }
        });
    });

    $('#pop-wrapper select.deptLv1').off('change').on('change', function () {
        var id = $(this).val();
        var htm = '<option value="">-请选择-</option>';
        if (!id) {
            $('#pop-wrapper select.deptLv2').html(htm);
            $('#pop-wrapper select').select0();
            return;
        }
        ajaxSubmitResponseJSON({
            url: 'dept/ajaxLoad.htm/' + id,
            success: function (data) {
                if (data.status == 0) {
                    var dataRows = data.data;
                    if (dataRows && dataRows.length > 0) {
                        for (var i = 0; i < dataRows.length; i++) {
                            htm += '<option value="' + dataRows[i].id + '">' + dataRows[i].name + '</option>';
                        }
                        $('#pop-wrapper select.deptLv2').html(htm);
                        $('#pop-wrapper select').select0();
                    }
                }
            }
        });
    });

    /*保存按钮事件*/
    $('#pop-wrapper .btn-4-save').off('click').on('click', function () {
        var url = 'manager/add.htm';
        var id = $('#pop-wrapper input[name=id]').val();
        if (id) {
            url = 'manager/modify.htm';
        }
        var departmentLv1Id = $('#pop-wrapper select.deptLv1').val();
        var departmentLv2Id = $('#pop-wrapper select.deptLv2').val();
        var departmentId;
        if (departmentLv1Id) {
            departmentId = departmentLv1Id;
        }
        if (departmentLv2Id) {
            departmentId = departmentLv2Id;
        }
        $('#pop-wrapper input[name=departmentId]').val(departmentId);
        ajaxSubmitResponseJSON({
            url: url,
            method: 'POST',
            data: $('#pop-wrapper form.common-form').serialize(),
            success: function (data) {
                if (data.status == 0) {
                    closeLayer();
                    $('#data-wrapper .btn-4-query').trigger('click');
                }
            }
        });
    });

});